package zmmShop

import (
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/tiger1103/gfast/v3/api/v1/zmm"
	sysservice "github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/dao"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/model/entity"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
	"math"
	"time"
)

func init() {
	service.RegisterZmmShop(New())
}

func New() *sZmmShop {
	return &sZmmShop{}
}

type sZmmShop struct {
}

func (s *sZmmShop) GetList(ctx context.Context, req *zmm.ZmmShopListReq) (list []*entity.ZmmShop, err error) {

	userInfo := sysservice.Context().GetLoginUserWithRole(ctx)
	if userInfo == nil {
		return nil, gerror.New("当前用户未登录或登录已失效")
	}

	where := g.Map{}
	// 普通店员只能看自己店的订单
	for _, role := range userInfo.RoleList {
		if role.Id == 3 {
			if userInfo.ShopId > 0 {
				where["id = ?"] = userInfo.ShopId
			} else {
				where["id = ?"] = "99999999"
			}
			break
		}
	}

	if req.Name != "" {
		where["name like ?"] = "%" + req.Name + "%"
	}

	err = dao.ZmmShop.Ctx(ctx).Where(where).Scan(&list)
	if err != nil {
		return
	}
	return
}

func (s *sZmmShop) Edit(ctx context.Context, req *zmm.ZmmShopEditReq) (id uint64, err error) {
	save, err := dao.ZmmShop.Ctx(ctx).Save(req)
	if err != nil {
		liberr.ErrIsNil(ctx, err, "修改店铺失败")
	}
	if req.ID < 1 {
		id, err := save.LastInsertId()
		if err != nil {
			return 0, err
		}
		req.ID = uint64(id)
	}
	return req.ID, err
}

func (s *sZmmShop) Delete(ctx context.Context, req *zmm.ZmmShopDelReq) error {
	_, err := dao.ZmmShop.Ctx(ctx).Where(dao.ZmmShop.Columns().Id, req.ID).Delete()
	liberr.ErrIsNil(ctx, err, "删除店铺失败")
	return err
}

func (s *sZmmShop) GetStatistics(ctx context.Context, req *zmm.ZmmShopStatisticsReq) (list *zmm.ZmmShopStatisticsRes, err error) {
	//list.Data = make(map[string]interface{})
	list = new(zmm.ZmmShopStatisticsRes)

	where := g.Map{}
	if req.ShopId != "" {
		where["shop_id = ?"] = req.ShopId
	}

	diffDay := int(math.Abs(float64(req.StartTime.Timestamp()-req.EndTime.Timestamp())) / 86400)
	starttime := req.StartTime
	var date []string
	var moneysum []float64
	var ordercount []int
	var membercount []int

	diffDay += 1

	for i := 0; i < diffDay; i++ {

		endtime := starttime.Format("Y-m-d 23:59:59")
		starttimes := starttime.Format("Y-m-d 00:00:00")

		date = append(date, starttime.Format("Y-m-d"))
		//glog.Print(ctx, "abcddddd", starttimes, endtime)

		// 收入趋势
		sum, _ := dao.ZmmMoneyLogShop.Ctx(ctx).Where(where).Where("created_time >= ?", starttimes).Where("created_time <= ?", endtime).Sum("money")
		moneysum = append(moneysum, sum)

		// 订单趋势
		ocount, _ := dao.ZmmOrder.Ctx(ctx).Where(where).Where("created_at >= ?", starttimes).Where("created_at <= ?", endtime).Count("id")
		ordercount = append(ordercount, ocount)

		// 会员趋势
		mcount, _ := dao.ZmmMember.Ctx(ctx).Where(where).Where("join_time >= ?", starttimes).Where("join_time <= ?", endtime).Count("id")
		membercount = append(membercount, mcount)

		starttime = starttime.Add(time.Duration(86400) * time.Second)

	}

	// 总金额
	all_money, _ := dao.ZmmMoneyLogShop.Ctx(ctx).Where(where).Where("created_time >= ?", req.StartTime.Format("Y-m-d 00:00:00")).Where("created_time <= ?", req.EndTime.Format("Y-m-d 23:59:59")).Sum("money")
	// 会员数
	all_m_count, _ := dao.ZmmMember.Ctx(ctx).Where(where).Where("join_time >= ?", req.StartTime.Format("Y-m-d 00:00:00")).Where("join_time <= ?", req.EndTime.Format("Y-m-d 23:59:59")).Count("id")
	// 订单数
	all_o_count, _ := dao.ZmmOrder.Ctx(ctx).Where(where).Where("created_at >= ?", req.StartTime.Format("Y-m-d 00:00:00")).Where("created_at <= ?", req.EndTime.Format("Y-m-d 23:59:59")).Count("id")

	list = &zmm.ZmmShopStatisticsRes{
		"date":         date,
		"date_money":   moneysum,
		"date_o_count": ordercount,
		"date_m_count": membercount,
		"all_m_count":  all_m_count,
		"all_o_count":  all_o_count,
		"all_money":    all_money,
	}

	return
}
